package com.activity.shop;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.activity.App;
import com.activity.adapter.SheetDetailAdapter;
import com.api.param.RecordPageParam;
import com.api.viewModel.StoreVM;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.activity.setting.BaseListViewActivity;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.google.gson.Gson;

public class SheetDetailActivity extends BaseListViewActivity<StoreVM> {
    private SheetDetailAdapter sheetDetailAdapter;
    RecordPageParam pageParam =new RecordPageParam();
    private int type;

    @Override
    protected int titleText() {
        return R.string.kaoqin_baobiao_xiangqing;
    }

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {
        super.created(savedInstanceState);
        type =getIntent().getExtras().getInt("type");
        if(type==1){
            setTitleText("外出报表详情");
        }
        if(type==2){
            setTitleText("加班报表详情");
        }
        pageParam.storeno = App.getInstance().userstoreinfoBean.getStoreno();

        pageParam.date = getIntent().getExtras().getString("param");
        sheetDetailAdapter = new SheetDetailAdapter(this);
        recycleView.setAdapter(sheetDetailAdapter);


        listViewModel.datRecord.observe(this,data->{

            if(data.isHeader){

                sheetDetailAdapter.getData().clear();
                sheetDetailAdapter.getData().addAll(data.getResult());

                headerFreshEnd();
            }else
            {
                sheetDetailAdapter.getData().addAll(data.getResult());
                if(data.getResult().size()>0){
                    footerFreshEnd();
                }else
                {
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
            sheetDetailAdapter.notifyDataSetChanged();

            if(sheetDetailAdapter.getData().size()>0){
                showEmpty(false);
            }else {
                showEmpty(true);
            }


        });
        listViewModel.datRecordError.observe(this,res->{
            if(res){
                headerFreshEnd();
            }else {
                pageParam.pagenum = (Integer.valueOf(pageParam.pagenum)-1)+"";
                footerFreshEnd();
            }
        });



        sheetDetailAdapter.setOnChildClickListener(new GroupedRecyclerViewAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(GroupedRecyclerViewAdapter adapter, BaseViewHolder holder, int groupPosition, int childPosition) {


                Bundle bundle = new Bundle();

                Gson gson = new Gson();
                bundle.putString("param",gson.toJson(sheetDetailAdapter.getData().get(childPosition)));
                bundle.putInt("type",type);
                ActivityUtil.startActivity(SheetDetailActivity.this,ClockDetailActivity.class,bundle);
            }
        });
        headerFreshing();

    }

    @Override
    protected void headerFreshing() {
        pageParam.pagenum ="1";

        refreshLayout.resetNoMoreData();
        addSubscription(listViewModel.getDateRecord(pageParam,true,type));
    }

    @Override
    protected void footerFreshing() {
        pageParam.pagenum = (Integer.valueOf(pageParam.pagenum)+1)+"";

        addSubscription(listViewModel.getDateRecord(pageParam,false,type));
    }

}
